#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/ioctl.h>

typedef struct erow {
    int idx;
    int size;
    int rsize;
    char *chars;
    char *render;
    unsigned char *hl;
    int hl_oc;
} erow;

struct editorConfig {
    int cx,cy;
    int rowoff;
    int coloff;
    int screenrows;
    int screencols;
    int numrows;
    int rawmode;
    erow *row;
    int dirty;
    char *filename;
    char statusmsg[80];
    time_t statusmsg_time;
    struct editorSyntax *syntax;
};

static struct editorConfig E;

int getWindowSize(int ifd, int ofd, int*rows, int*cols) {
    struct winsize ws;

    if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws)==0){
        *cols = ws.ws_col;
        *rows = ws.ws_row;
    }
    printf("%d %d\n",*rows,*cols);
    return 0;
}

void updateWindowSize(void) {
    if(getWindowSize(STDIN_FILENO,STDOUT_FILENO,&E.screenrows,&E.screencols) == -1) {
        perror("Unable to query the screen for size (columns / rows)");
    }
}

void initEditor(void) {
    E.cx = 0;
    E.cy = 0;
    E.rowoff = 0;
    E.coloff = 0;
    E.numrows = 0;
    E.row = NULL;
    E.dirty = 0;
    E.filename = NULL;
    E.syntax = NULL;
    updateWindowSize();
}

int main(int argc, char **argv)
{
    if (argc != 2) {
        fprintf(stderr,"Usage: kilo <filename>\n");
        exit(1);
    }

    initEditor();
    return 0;
}